/**
 * Created by loso on 2017/3/19.
 */
public class _400_NthDigit {
    //找到数字的总对应位数是多少
    public static void main(String[] args) {
        _400_NthDigit a = new _400_NthDigit();
        System.out.println(a.findNthDigit(1000000000));
    }
    public int findNthDigit(int n) {
        if (n<10){
            return n;
        }
        long[] countNum = new long[11];
        countNum[0] = 9;
        for (int i = 1; i < 11; i++) {
            countNum[i] = 9*(long) Math.pow(10,i)*(i+1) + countNum[i-1];
        }
        int bit = 0;
        for (int i = 0; i < 10; i++) {
            if(n<=countNum[i]){
                break;
            }
            bit++;
        }
        int num = (int)Math.pow(10,bit) + (n - (int)countNum[bit-1] -1)/(bit+1) ;
        int numbit = ((n - (int)countNum[bit-1])-1)%(bit+1);
return Integer.toString(num).charAt(numbit)-'0';
    }

    public int findNthDigitBigboss(int n) {
        int len = 1;
        long count = 9;
        int start = 1;

        while (n > len * count) {
            n -= len * count;
            len += 1;
            count *= 10;
            start *= 10;
        }

        start += (n - 1) / len;
        String s = Integer.toString(start);
        return Character.getNumericValue(s.charAt((n - 1) % len));
    }
}
